package com.heima.common.aliyun;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;
import com.aliyun.imageaudit20191230.models.ScanTextResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.heima.common.aliyun.util.ClientUploader;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.*;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenImageScanFree {

    private String accessKeyId;
    private String secret;

    public Map greenImageScanFree(String imageUrl) throws Exception {

        if(imageUrl.isEmpty()){
            return null;
        }
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(secret);
        // 访问的域名
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";

        com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config);

        com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask task0 = new com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask()
                .setImageURL(imageUrl)
                .setDataId(UUID.randomUUID().toString())
                .setImageTimeMillisecond(new Date().getTime());
        com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
                .setScene(java.util.Arrays.asList(
                        "porn",
                        "terrorism",
                        "ad",
                        "live"
                ))
                .setTask(java.util.Arrays.asList(
                        task0
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            Map resultMap = new HashMap<>();
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.imageaudit20191230.models.ScanImageResponse scanImageResponse = client.scanImageWithOptions(scanImageRequest, runtime);
            if(200 == scanImageResponse.getStatusCode()){
                List<ScanImageResponseBody.ScanImageResponseBodyDataResults> results = scanImageResponse.getBody().getData().getResults();
                if(!results.isEmpty() && results.size() > 0){
                    for (ScanImageResponseBody.ScanImageResponseBodyDataResults result : results) {
                        List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = result.getSubResults();
                        if(!subResults.isEmpty() && subResults.size() > 0 ){
                            for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults subResult : subResults) {
                                String suggestion = subResult.getSuggestion();
                                String label = subResult.getLabel();
                                if(!suggestion.equals("pass")){
                                    resultMap.put("suggestion",suggestion);
                                    resultMap.put("label",label);
                                    return resultMap;
                                }
                            }
                        }
                    }
                    resultMap.put("suggestion","pass");
                    return resultMap;
                } else {
                    return null;
                }
            }else{
                return null;
            }
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }
}
